import { 
    GET_GOOD_LIST, 
    GET_CATE_LIST, 
    UPDATE_GOOD, 
    GET_GOOD_INFO, 
    CLEAR_CACHE} from '../actionType'
interface GoodState{
    total:number,
    list:Array<object>,
    cates:Array<object>,
    count:number,
    info:object
}

const initState:GoodState={
    total:0,
    list:[],
    cates:[],
    count:0,//只是用于走流程表示商品新增或编辑成功
    info:{}//用来缓存需要编辑的物品信息
}

export default function reducer(state=initState,{type,payload}) {
    let newState=JSON.parse(JSON.stringify(state))
    
    switch(type){
        case GET_GOOD_LIST:
        newState.total=payload.total
        newState.list=payload.list
        break
        case GET_CATE_LIST:
        newState.cates=payload.list
        break
        case UPDATE_GOOD:
        newState.count++
        break
        case GET_GOOD_INFO:
        newState.info=payload
        break
        case CLEAR_CACHE:
        newState.info={}
        newState.count=0
        break
    }
    return newState
}